package com.gitee.ly.chain.test;

import com.alibaba.fastjson.JSON;
import com.gitee.ly.chain.Flow;
import com.gitee.ly.chain.Form;
import com.gitee.ly.chain.Link;
import com.gitee.ly.chain.abstractclass.GlobalTool;
import com.gitee.ly.chain.test.form.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ChainTestMain
 *
 * @author ly
 **/
public class ChainTestMain {

    private static List<Form> loadForm() {
        List<Form> formList = new ArrayList<>();
        formList.add(new CkjhdForm());
        formList.add(new KclsForm());
        formList.add(new RkdForm());
        formList.add(new RkjhdForm());
        formList.add(new SjdForm());
        formList.add(new KcmxForm());
        return formList;
    }

    public static void main(String[] args) {
        System.out.println("start main success");
        //表单
        List<Form> formList = loadForm();
        //根据配置，加载链
        //GlobalTool.setLogInfoHandle(data -> LoggerFactory.getLogger("chain").info(data));
        GlobalTool.setLogInfoHandle(System.out::println);
        GlobalTool.setJsonSerialHandle(JSON::toJSONString);
        GlobalTool.loadChain(formList);

        //这样执行
        Flow<Map> shlc = GlobalTool.getFlow("SHLC", Map.class);
        shlc.excute(new HashMap() {{
            put("a", "数据1");
            put("b", 123);
        }});
        //也可以这样执行
        GlobalTool.getFlow("SHLC", String.class).excute("数据2");

        //查询流程下的环节有哪些
        for (Flow flow : GlobalTool.getAllFlow()) {
            System.out.println("Flow:" + "  " + flow.getType() + "  " + flow.getName());
            List<Link> links = GlobalTool.getLinkListByFlowType(flow.getType());
            if (links == null) {
                continue;
            }
            for (Link link : links) {
                System.out.println("    Link:" + "  " + link.getSort() + "  " + link.getType() + "  " + link.getName());
                //查询环节下的表单有哪些
                List<Form> forms = link.allForm();
                for (Form form : forms) {
                    System.out.println("        Form:" + form.getType() + "  " + form.getName());
                }
            }
        }

    }

}
